const express=require('express');
const pool=require('../pool.js');
var router=express.Router();
//1.添加预约
router.post('/',(req,res,next)=>{
    var obj=req.body;
		
    if(!req.session.loginUid){
        req.session.pageToJump='c.html';
        req.session.toBuyDid=obj.did;
        req.session.toc_count=obj.c_count;
        res.send({code:300,msg:'login required'});
        return;
    }
    var sql=`SELECT oid FROM xh_order WHERE o_uid=? AND o_did=?`;
    pool.query(sql,[req.session.loginUid,$did],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        var sql2;
        if(result.length>0){
            sql2=`UPDATE xh_order SET count=count+1 WHERE o_uid=${req.session.loginUid} AND did=${$did}`;

        }else{
            sql2=`INSERT INTO xh_order VALUES(NULL,1,${$c_count},null,null, ${req.session.loginUid},${$did})`;
        }
        pool.query(sql2,(err,result2)=>{
            if(err) {
                next(err);
                return;
            }
            if(result2.affectedRows>0){
                res.send({code:200,msg:'add suc'});
            }else{
                res.send({code:500,msg:'add err'});
            }
        });
    });
});

//2.预约列表
router.get('/list',(req,res,next)=>{
    var output={};
    if(!req.session.loginUid){
        req.session.pageToJump='cart.html';
        res.send({code:300,msg:'login required'});
        return;
    }
    var sql='SELECT oid,did,title,park_time,park_site,c_count FROM xh_detail , xh_order WHERE o_uid=?';
    pool.query(sql,[req.session.loginUid],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        output.code=200;
        output.data=result;
        for(var i=0;i<output.data.length;i++){
            var did=output.data[i].did;
            (function(did,i){
              pool.query('SELECT img FROM xh_scenic WHERE sid=? LIMIT 1',[did],(err,result)=>{
                output.data[i].img=result[0].img;
              });
            })(did,i);
        }
        setTimeout(() => {
            res.send(output);
        }, 100);
    });
});

//3.取消预约
router.delete('/:oid',(req,res)=>{
    var obj=req.params;
    if(!obj.oid){
        res.send({code:401,msg:'oid required'});
        return;
    }
    if(!req.session.loginUid){
        res.send({code:300,msg:'login required'});
        return;
    }
    pool.query('DELETE FROM xh_order WHERE oid=?',[obj.oid],(err,result)=>{
        if(err) throw err;
        if(result.affectedRows>0){
            res.send({code:200,msg:'delete succ'});
        }else{
            res.send({code:500,msg:'delete err'});
        }
    });
});

//4.修改预约条目中的预约数量
router.put('/count',(req,res,next)=>{
    var obj=req.body;
    if(!obj.oid){
        res.send({code:401,msg:'oid required'});
        return;
    }
    if(!obj.c_count){
        res.send({code:402,msg:'count required'});
        return;
    }
    if(!req.session.loginUid){
        res.send({code:300,msg:'login required'});
        return;
    }
    pool.query('UPDATE xh_order SET c_count=? WHERE oid=?',[obj.c_count,obj.oid],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        if(result.affectedRows>0){
            res.send({code:200,msg:'update succ'});
        }else{
            res.send({code:500,msg:'update err'});
        }
    });
});

//5.获取当前登录用户的预约内容
router.get('/listchecked',(req,res)=>{
    var output={};
    if(!req.session.loginUid){
        res.send({code:300,msg:'login required'});
        return;
    }
    var sql = "SELECT oid,did,title,c_count,order_time,start FROM xh_detail , xh_order WHERE  o_uid=?";
    pool.query(sql,[req.session.loginUid],(err,result)=>{
        if(err) throw err;
        output.code=200;
        output.data=result[0];
        for(var i=0;i<output.data.length;i++){
            var did=output.data[i].did;
            (function(did,i){
              pool.query('SELECT img FROM xh_scenic WHERE sid=? LIMIT 1',[did],(err,result)=>{
                output.data[i].img=result[0].img;
              });
            })(did,i);
        }
        setTimeout(() => {
            res.send(output);
          }, 100);
    });
});

module.exports=router;